<style scoped lang="scss">
@import url('index.scss');
</style>

<template>
  <div class="app-container">

    <!--新模版样式-->
    <div class="topsearch">
      <el-row>
        <el-col :span="22" class="searchcol">
          <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="科室名称" prop="kname">
              <el-input v-model="queryParams.kname" placeholder="请输入科室名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="拼音码" prop="pycode">
              <el-input v-model="queryParams.pycode" placeholder="请输入拼音码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="院区" prop="hoscampus">
              <el-input v-model="queryParams.hoscampus" placeholder="请输入院区" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="科室主任" prop="departmenthead">
              <el-input v-model="queryParams.departmenthead" placeholder="请输入科室主任" clearable
                @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="科室电话" prop="contactphone">
              <el-input v-model="queryParams.contactphone" placeholder="请输入科室电话" clearable @keyup.enter="handleQuery" />
            </el-form-item> -->
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22" class="rightcol">
          <div class="newbtn" @click="handleAdd" v-hasPermi="['system:department:add']">
            <img src="@/assets/images/add.svg" alt="">
            <span>新建</span>
          </div>
          <div class="newbtn" v-if="single == false" @click="handleUpdate" v-hasPermi="['system:department:edit']">
            <img src="@/assets/images/edit.svg" alt=""></img>
            <span>编辑</span>
          </div>
          <div class="newbtn" v-if="multiple == false" @click="handleDelete" v-hasPermi="['system:department:remove']">
            <img src="@/assets/images/del.svg" alt=""></img>
            <span>删除</span>
          </div>
          <div class="newbtn" @click="handleExport" v-hasPermi="['system:department:export']">
            <img src="@/assets/images/export.svg" alt=""></img>
            <span>导出</span>
          </div>
          <div class="newbtn" @click="getList">
            <img src="@/assets/images/refresh.svg" alt=""></img>
            <span>刷新</span>
          </div>
        </el-col>
        <!-- <el-col :span="2">
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-col> -->
      </el-row>
    </div>

    <!-- 
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['system:department:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:department:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:department:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['system:department:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->
    <div class="newtable">
      <el-table v-loading="loading" :data="departmentList" @selection-change="handleSelectionChange"
        :cell-style="{ backgroundColor: '#f9f9f9' }">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="科室ID" width="60" align="center" prop="kid" />
        <el-table-column label="科室名称" align="center" prop="kname" />
        <el-table-column label="拼音码" align="center" prop="pycode" />
        <el-table-column label="院区" align="center" prop="hoscampus" />
        <el-table-column label="科室楼层" align="center" prop="deptfloor" />
        <el-table-column label="所在区域" align="center" prop="deptregion" />
        <el-table-column label="科室图片" align="center" prop="ksimage" width="100">
          <template #default="scope">
            <image-preview :src="scope.row.ksimage" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="科室类型" align="center" prop="ktype">
          <template #default="scope">
            {{ ha_dept_type.find(item => item.value == scope.row.ktype)?.label || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="科室主任" align="center" prop="departmenthead" />
        <el-table-column label="科室电话" align="center" prop="contactphone" />
        <!-- <el-table-column label="科室描述" align="center" prop="description" /> -->
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <el-tag :type="scope.row.status == '1' ? 'success' : 'danger'">{{ scope.row.status == '1' ? '正常' : '停用'
              }}</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:department:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:department:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />

    </div>


    <!-- 添加或修改科室管理对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" class="newDialog">
      <el-form ref="departmentRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="科室图片" prop="ksimage">
          <image-upload v-model="form.ksimage" />
        </el-form-item>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="科室名称" prop="kname">
              <el-input v-model="form.kname" placeholder="请输入科室名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="科室类型" prop="ktype">
              <!-- <el-input v-model="form.ktype" placeholder="请输入科室类型" /> -->
              <el-select v-model="form.ktype" placeholder="请选择科室类型">
                <el-option v-for="item in ha_dept_type" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="院区" prop="hoscampus">
              <el-input v-model="form.hoscampus" placeholder="请输入院区" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="拼音码" prop="pycode">
              <el-input v-model="form.pycode" placeholder="请输入拼音码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="科室楼层" prop="deptfloor">
              <el-input v-model="form.deptfloor" placeholder="请输入科室楼层" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在区域" prop="deptregion">
              <el-input v-model="form.deptregion" placeholder="请输入所在区域" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="科室主任" prop="departmenthead">
              <el-input v-model="form.departmenthead" placeholder="请输入科室主任" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="科室电话" prop="contactphone">
              <el-input v-model="form.contactphone" placeholder="请输入科室电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="科室描述" prop="description">
          <el-input v-model="form.description" type="textarea" :rows="4" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in ha_normal_status" :key="dict.value" :value="dict.value" :label="dict.value">{{
              dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Department">
import { listDepartment, getDepartment, delDepartment, addDepartment, updateDepartment } from "@/api/system/department"

const { proxy } = getCurrentInstance()
const { ha_dept_type } = proxy.useDict('ha_dept_type'); // 使用字典
const { ha_normal_status } = proxy.useDict('ha_normal_status'); // 使用字典

const departmentList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    kname: null,
    pycode: null,
    hoscampus: null,
    deptfloor: null,
    deptregion: null,
    ksimage: null,
    ktype: null,
    departmenthead: null,
    contactphone: null,
    description: null,
    status: null,
  },
  rules: {
    kname: [{ required: true, message: '请输入科室名称', trigger: 'blur' }],
    ktype: [{ required: true, message: '请选择科室类型', trigger: 'change' }],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询科室管理列表 */
function getList() {
  loading.value = true
  listDepartment(queryParams.value).then(response => {
    departmentList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    kname: null,
    kid: null,
    pycode: null,
    hoscampus: null,
    deptfloor: null,
    deptregion: null,
    ksimage: null,
    ktype: null,
    departmenthead: null,
    contactphone: null,
    description: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    status: '1',
    remark: null
  }
  proxy.resetForm("departmentRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.kid)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加科室管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _kid = row.kid || ids.value
  getDepartment(_kid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改科室管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["departmentRef"].validate(valid => {
    if (valid) {
      if (form.value.kid != null) {
        updateDepartment(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addDepartment(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _kids = row.kid || ids.value
  proxy.$modal.confirm('是否确认删除科室管理编号为"' + _kids + '"的数据项？').then(function() {
    return delDepartment(_kids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/department/export', {
    ...queryParams.value
  }, `department_${new Date().getTime()}.xlsx`)
}

getList()
</script>
